<?php

namespace app\admin\controller;

use taobao\AliOss;

class Upload extends Common {

	//图片上传
    public function upload() {
       $file = request()->file('file');
       $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads/images');
       if($info){
            echo $info->getSaveName();
        }else{
            echo $file->getError();
        }
    }

    //图片上传至OSS
    public function uploadToOss() {
        $tempFile = $_FILES['file']['tmp_name'];
        $fileName = md5(uniqid(mt_rand(), true)).".".strtolower(pathinfo($_FILES['file']['name'])["extension"]);
        $info = AliOss::uploadContent(file_get_contents($tempFile),"image/".$fileName);
        //echo AliOss::getFileUrl("image/".$fileName);
        echo $info["info"]["url"];
    }

    //音频上传至OSS
    public function uploadAudioToOss() {

        $file = request()->file('file');

        $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads' . DS .'audio');

        if($info){

            $fileName =  $_SERVER['DOCUMENT_ROOT'] . DS . 'uploads' . DS .'audio'. DS . $info->getSaveName();

            AliOss::uploadFile($fileName,"audio/".explode(DS,$info->getSaveName())[1]);

            $getID3 = new \getID3();

            $fileInfo = $getID3->analyze($fileName);

            $returnData["code"] = 200;
            $returnData["msg"] = "";
            $returnData["time"] = $_SERVER['REQUEST_TIME'];

            $returnData["data"]["url"] = AliOss::getFileUrl("audio/".explode(DS,$info->getSaveName())[1]);
            $returnData["data"]["playtime_seconds"] = round($fileInfo["playtime_seconds"],2);

            unset($fileName);

        }else{
            $returnData["code"] = -1;
            $returnData["msg"] = $file->getError();
            $returnData["time"] = $_SERVER['REQUEST_TIME'];
            $returnData["data"] = "";
        }

        echo json_encode($returnData);

    }

    // 图片上传至oss by xcj
    public function uploadImgToOss() {
        $tempFile = $_FILES['file']['tmp_name'];

        $fileName = md5(uniqid(mt_rand(), true)).".".strtolower(pathinfo($_FILES['file']['name'])["extension"]);

        $info = AliOss::uploadFile($tempFile,$fileName);

        $url = $info['info']['url'];
        $start = strrpos($url,"/");
        $imgName = substr($url, $start);
        return config('oss.Cname').$imgName;
//        return $this->getOssImgUrl($imgName);
    }

    //会员头像上传
    public function uploadface() {
       $file = request()->file('file');
       $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads/face');
       if($info){
            echo $info->getSaveName();
        }else{
            echo $file->getError();
        }
    }

}